package com.jushu.storbox;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jushu.storbox.adapter.LookImageAdapter;
import com.jushu.storbox.constant.MessageCode;
import com.jushu.storbox.manager.ActivityTaskManager;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.util.image.BitmapUtil;

@SuppressLint("HandlerLeak")
public class LookImageActivity extends Activity {
	private static final String LOG_TAG = "LookImageActivity";
	private RelativeLayout navLayout, textLayout;
	private ViewPager viewPager;
	private TextView size;
	private CheckBox select;
	private Button confirm;
	private int selectIndex = 0;
	@SuppressLint("UseSparseArrays")
	private HashMap<Integer, Boolean> selectStruts = new HashMap<Integer, Boolean>();

	private int currentPosition;
	private List<String> imageUrlList;
	private boolean isdisplayNav = true;
	private int selectSize = 0;
	
	//cloud bitmaps recycle
	private List<SoftReference<Bitmap>> browserBitmaps = new ArrayList<SoftReference<Bitmap>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.look_image_layout);
		//
		ActivityTaskManager.getInstance().putActivity(LOG_TAG, LookImageActivity.this);
		//
		Bundle bundle = getIntent().getExtras();
		imageUrlList = bundle.getStringArrayList("allImageUrl");
		currentPosition = bundle.getInt("position");
		initView();
		initData();
		setViewListener();
		initState();
	}

	private void initState() {
		if(imageUrlList.size() == 1){
			selectStruts.put(0, true);
			select.setChecked(true);
			select.setVisibility(View.INVISIBLE);
			confirm.setText("	" + getString(R.string.upload) + "	");
		}
		
	}

	private void initView() {
		navLayout = (RelativeLayout) findViewById(R.id.look_image_nav_layout);
		textLayout = (RelativeLayout) findViewById(R.id.look_image_text_layout);
		viewPager = (ViewPager) findViewById(R.id.imageSwitcher1);
		size = (TextView) findViewById(R.id.look_image_size);
		confirm = (Button) findViewById(R.id.look_image_comfirm_button);
		select = (CheckBox) findViewById(R.id.look_image_checkbox);
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				if (isdisplayNav) {
					navLayout.setVisibility(View.GONE);
					textLayout.setVisibility(View.GONE);
					isdisplayNav = false;
				} else {
					navLayout.setVisibility(View.VISIBLE);
					textLayout.setVisibility(View.VISIBLE);
					isdisplayNav = true;
				}
			}
		}
	};

	private void initData() {
		LookImageAdapter adapter = new LookImageAdapter(
				LookImageActivity.this, imageUrlList, handler, browserBitmaps);
		viewPager.setAdapter(adapter);
		viewPager.setCurrentItem(currentPosition);
		size.setText((currentPosition + 1) + "/" + imageUrlList.size());
		confirm.setText(LookImageActivity.this.getResources().getString(
				R.string.upload)
				+ "(" + selectSize + "/" + 10 + ")");
		for (int i = 0; i < imageUrlList.size(); i++) {
			selectStruts.put(i, false);
		}
	}

	private void setViewListener() {
		viewPager.setOnPageChangeListener(new ViewPageChangeListener());
		select.setOnClickListener(new SelectChecked());
		confirm.setOnClickListener(new ConfirmClickListener());
	}

	private class ConfirmClickListener implements OnClickListener {
		@Override
		public void onClick(View arg0) {
			//
			ArrayList<String> uploadList = new ArrayList<String>();
			for (int i = 0; i < selectStruts.size(); i++) {
				if (selectStruts.get(i)) {
					uploadList.add(imageUrlList.get(i));
				}
			}
			if (uploadList.size() != 0) {
				if (!ShareDriveApplication.getInstance()
						.getWifiController().getNetworkState()) {

					Toast.makeText(LookImageActivity.this,
							LookImageActivity.this.getString(R.string.allfile_search_is_not_vailible),
							Toast.LENGTH_SHORT).show();
				} else {
					Intent data = new Intent();
					Bundle bundle = new Bundle();
					bundle.putStringArrayList("resultUIL", uploadList);
					data.putExtra("result", bundle);
					setResult(MessageCode.UPLOAD_SELECT_FILE_RESULTCODE, data);
					Toast.makeText(
							LookImageActivity.this,
							LookImageActivity.this.getResources().getString(
									R.string.image_successfully_added),
							Toast.LENGTH_SHORT).show();

				}
				LookImageActivity.this.finish();
			} else {
				Toast.makeText(
						LookImageActivity.this,
						LookImageActivity.this.getResources().getString(
								R.string.please_selete_image),
						Toast.LENGTH_LONG).show();
			}
		}
	}

	class SelectChecked implements OnClickListener
	{
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			if(selectStruts.get(selectIndex))
			{
				if(selectSize>0)
				{
					selectStruts.put(selectIndex, ((CheckBox)arg0).isChecked());
					selectSize -= 1;
				}
				confirm.setText(LookImageActivity.this.getResources().getString(R.string.upload)+ "("+ selectSize+ "/"+ 10 + ")");
			}
			else
			{
				if (selectSize > 10 || selectSize == 10)
				{
					Toast.makeText(LookImageActivity.this,LookImageActivity.this.getResources().getString(R.string.select_image_prompt),Toast.LENGTH_LONG).show();
					select.setChecked(false);
				}
				else
				{
					selectStruts.put(selectIndex, ((CheckBox)arg0).isChecked());
					selectSize = 0;
					for (int i = 0; i < selectStruts.size(); i++) 
					{
						if (selectStruts.get(i))
						{
							selectSize += 1;
						}
					}
					confirm.setText(LookImageActivity.this.getResources().getString(R.string.upload)+ "("+ selectSize+ "/"+ 10 + ")");
				}
			}
		}
	}
	private class ViewPageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {
			size.setText((arg0 + 1) + "/" + imageUrlList.size());
			selectIndex = arg0;
			select.setChecked(selectStruts.get(selectIndex));
		}
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		ActivityTaskManager.getInstance().removeActivity(LOG_TAG);
		LogUtil.i(LOG_TAG, "[onDestroy]browserBitmaps:" + browserBitmaps.size() + ", recycle browserBitmaps...");
		BitmapUtil.recycleBitmapList(browserBitmaps);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		LogUtil.i(LOG_TAG, newConfig.orientation+"");
		super.onConfigurationChanged(newConfig);
	}
	
}
